﻿import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import { Tab } from './tab.model';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementCommissionItemEditComponent } from './requirement-commission-item_edit.component';
import { RequirementsForPartsDetailDto, TestDataDto, RequirementProductionServiceProxy,StockForStandardsServiceProxy, TaskForStandardPartsServiceProxy } from '@shared/service-proxies/service-proxies';
import { RequirementCommissionTabComponent } from './requirement-commission_tab.component';

@Component({
    templateUrl: './requirement-commission.component.html',
    styleUrls: ['./requirement-commission.component.less'],
    animations: [appModuleAnimation()]
})
export class RequirementCommissionComponent extends AppComponentBase {
    @ViewChild('requirementCommissionItemEditComponent') requirementCommissionItemEditComponent: RequirementCommissionItemEditComponent;
    @ViewChild('requirementCommissionTabComponent') requirementCommissionTabComponent: RequirementCommissionTabComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    tabs: Array<Tab>;
    tabObject: {
        filterText: ''
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';
    hidden = true;

    constructor(
        injector: Injector,
        private _requirementProductionServiceProxy: RequirementProductionServiceProxy,
        private _stockForStandardsServiceProxy: StockForStandardsServiceProxy,
        private _taskForStandardPartsServiceProxy: TaskForStandardPartsServiceProxy) {
        super(injector);
        this.tabs = new Array<Tab>();
    }
    /**
     * 弹出标准件投产历史tab页面
     * @param requirementsForPartsDetailDto
     */
    addTab(requirementsForPartsDetailDto: RequirementsForPartsDetailDto): void {
        if (this.tabs.length == 0) {
            let tab: Tab = new Tab("需求投产历史【"+requirementsForPartsDetailDto.features.displayName+"】");
            tab.index = this.tabs.length;
            tab.standardPartsDetailId = requirementsForPartsDetailDto.standardPartsDetailId;
            tab.active = true;
            this.tabs.push(tab);
        } else {
            this.tabs[0].active = true;
            this.tabs[0].name = "需求投产历史【"+requirementsForPartsDetailDto.features.displayName+"】";
            this.tabs[0].standardPartsDetailId = requirementsForPartsDetailDto.standardPartsDetailId;
        }
        // 初始化检索
        if(this.requirementCommissionTabComponent){
            // 这里不一定能执行到
            this.requirementCommissionTabComponent.setStandardPartsDetailId(requirementsForPartsDetailDto.standardPartsDetailId);
            this.requirementCommissionTabComponent.getRequirementItems();

            this.requirementCommissionTabComponent.setParentComponent(this);
        }
        
    }
    /**
     * 清除tab页面
     */
    clearTab(): void {

    }
    createRequirementItem(tab:Tab,event?: LazyLoadEvent):void{
        
    }
    /**
     * 标准件需求投产
     * @param requirementsForPartsDetailDto 
     */
    createOrEditRequirementPackage(requirementsForPartsDetailDto: RequirementsForPartsDetailDto): void {
        this.requirementCommissionItemEditComponent.linkAdd(requirementsForPartsDetailDto);
    }
    deleteRequirementPackage(testDataDto :TestDataDto): void {
        this.message.confirm(
            '需求包将被删除',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._requirementProductionServiceProxy.deleteRequirementForTask(testDataDto.id)
                    .pipe(finalize(() => { this.getList();}))
                    .subscribe(() => {
                        this.notify.info(this.l('DeletedSuccessfully'));
                    });
                }
            }
        );
    }
    commitRequirementPackage(){
        this.message.confirm(
            '需求包将被提交',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                }
            }
        );
    }
    /**
     * 检索待投产需求
     * @param event 
     */
    getList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._requirementProductionServiceProxy.getRequirementsForPartsDetail(
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe().subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
                for(var key in result.items){
                    this.getStockCount(result.items[key]);
                    this.getUnOutCount(result.items[key]);
                }
            });

    }
    /**
     * 刷新页面
     */
    refresh(){
        this.getList();
    }
    getStockCount(record): void {
        this._stockForStandardsServiceProxy.getStockRemainingByDetailId(record.standardPartsDetailId)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                record.stockCount = result;
            });
    }
    getUnOutCount(record): void {
        this._taskForStandardPartsServiceProxy.getUnIntoQuantity(record.standardPartsDetailId)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                record.unOutCount = result;
            });
    }
   
    downloadWebLogs = function () {
        const self = this;
        self._webLogService.downloadWebLogs().subscribe((result) => {
            self._fileDownloadService.downloadTempFile(result);
        });
    };

    onResize(event): void {

    }

    ngOnInit(): void {
        const self = this;
    }
}
